package com.zat.mes.andrmes.base

import android.app.Dialog
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.love.zywanandroid.customerview.TitleBar
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.customerview.LoadingDialog

/**
 * @author zy
 * @date :2019-08-23 09:17
 * @description:父fragment
 */
abstract class BaseFragment : Fragment() {
    /**
     * 通用loading
     */
    protected var loadingDialog: Dialog? = null
    /**
     * title
     */
    private var titleBar: TitleBar? = null
    /**
     * 主布局
     */
    protected var contentView: LinearLayout? = null
    protected var layoutView: FrameLayout? = null;
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val content = inflater.inflate(getViewId(), container, false)
        contentView = inflater.inflate(R.layout.ll_decview, null) as LinearLayout
        if (isOpenTitleBar()) {
            titleBar = TitleBar(context)
            contentView?.addView(titleBar)
        }
        contentView?.addView(content)
        layoutView =
            LayoutInflater.from(context).inflate(R.layout.layout_content_view, null) as FrameLayout
        layoutView?.addView(contentView, 0)
        return layoutView
    }

    /**
     * 显示空布局
     */
    fun setEmptyVisible() {
        layoutView?.findViewById<TextView>(R.id.tvEmptyContent)?.visibility = View.VISIBLE
    }

    /**
     * 隐藏空布局
     */
    fun setEmptyGone() {
        layoutView?.findViewById<TextView>(R.id.tvEmptyContent)?.visibility = View.GONE
    }

    /**
     * 通用布局文件
     */
    abstract fun getViewId(): Int
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initView()
        initData()

    }

    /**
     * 是否显示titlebar
     */
    abstract fun isOpenTitleBar(): Boolean

    /**
     * 通用initview
     */
    abstract fun initView()

    /**
     * 通用initData
     */
    abstract fun initData()

    /**
     * 设置titleText
     */
    open fun setTitleText(text: String) {
        titleBar?.setTitleText(text)
    }

    /**
     * 关闭loading
     */
    open fun closeLoading() {
        loadingDialog?.let { LoadingDialog.closeLoading(it) }
    }

    /**
     * 显示loading
     */
    open fun showLoading() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.createLoading(context!!)
        } else {
            loadingDialog?.show()
        }
    }

    /**
     * fragment startActivity
     */
    open fun startFragmentActivity(intent: Intent) {
        try {
            startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            Log.e("activity", e.toString())
        }
    }

}